package com.banana.springcloud.service.impl;

import cn.hutool.core.util.IdUtil;
import com.banana.springcloud.service.StreamMessageProvider;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;
/**
 * @EnableBinding 注解是绑定channel和exchange绑定在一起
 * Source.class 定义消息的推送管道
 * 官网案例 https://cloud.spring.io/spring-cloud-static/spring-cloud-stream-binder-rabbit/3.0.1.RELEASE/reference/html/spring-cloud-stream-binder-rabbit.html
 * @author layman
 * @date 2021/1/21
 */
@EnableBinding(Source.class)
public class StreamMessageProviderImpl implements StreamMessageProvider {

    @Resource
    private MessageChannel output; //消息发送通道
    /**
     * 发送消息
     * @return string
     */
    @Override
    public String sendMessage() {
        String serial = IdUtil.simpleUUID();
        output.send(MessageBuilder.withPayload(serial).build());
        System.out.println("MessageProvider---生产者发送消息 ------>: "+serial);
        return serial;
    }
}
